﻿using Liang.DataBase.BLL;
using Liang.DataBase.Model.Entity;
using System;
using System.Linq;
using System.Windows.Forms;

namespace Client
{
    public partial class FrmOrganEdit : Form
    {
        public sys_organ organ { get; set; }

        private OrganBLL organBLL = new OrganBLL();

        private BasicdataBLL basicdataBLL = new BasicdataBLL();

        public FrmOrganEdit()
        {
            InitializeComponent();
        }

        public FrmOrganEdit(sys_organ organ)
        {
            InitializeComponent();
            this.organ = organ;
        }

        private void FrmOrganEdit_Load(object sender, EventArgs e)
        {
            intcombobox();
            init();
        }

        void intcombobox()
        {
            var list = basicdataBLL.FindAll(s => s.code.Contains("JG"));
            this.comOrganType.DataSource = list;
            this.comOrganType.DisplayMember = "des";
            this.comOrganType.ValueMember = "basicdataId";
        }

        void init()
        {
            if (organ == null)
            {
                this.groupBox1.Text = "机构信息-新增";
            }
            else
            {
                this.groupBox1.Text = "机构信息-修改";
                this.txtAddress.Text = organ.address;
                this.txtName.Text = organ.name;
                this.txtName.Enabled = false;
                var list = basicdataBLL.FindAll(s => s.basicdataId.Equals(organ.basicdataId));
                if(list.Count >0)
                {
                    var rid = list[0].basicdataId;
                    SetComboBoxSelectItemByValue(comOrganType, rid.ToString());
                }
            } 
        }

        void SetComboBoxSelectItemByValue(ComboBox cb, string Value)
        {
            for (int i = 0; i < cb.Items.Count; ++i)
            {
                sys_basicdata o = (sys_basicdata)cb.Items[i];

                if (o.basicdataId.ToString() == Value)
                {
                    cb.SelectedIndex = i;
                    break;
                }
            }
        }

        protected override void WndProc(ref Message m)
        {
            //if (m.Msg == 0xA1 && m.WParam.ToInt32() == 2)
            //    return;
            base.WndProc(ref m);
        }

        bool validate()
        {
            if (string.IsNullOrEmpty(txtName.Text))
            {
                MessageBox.Show("请输入机构名称");
                this.txtName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtAddress.Text))
            {
                MessageBox.Show("请输入地址");
                this.txtAddress.Focus();
                return false;
            }

            if (comOrganType.SelectedIndex == -1)
            {
                MessageBox.Show("请选择机构类型");
                this.comOrganType.Focus();
                return false;
            }
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (validate())
            {
                if (organ == null)
                {
                    organ = new sys_organ()
                    {
                        name = txtName.Text,
                        address = txtAddress.Text,
                        createTime = DateTime.Now,
                        basicdataId = ((sys_organ)comOrganType.SelectedItem).basicdataId
                    }; 
                    organBLL.Insert(organ);
                }
                else
                {
                    organ.name = txtName.Text;
                    organ.address = txtAddress.Text;
                    organ.createTime = DateTime.Now;
                    organ.basicdataId = ((sys_basicdata)comOrganType.SelectedItem).basicdataId;
                    organBLL.Update(organ, organ.organId);
                }

                DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
